﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Pomo
{
    /// <summary>
    /// Interaction logic for PomoWindow.xaml
    /// </summary>
    public partial class PomoWindow : Window
    {
        Pomodoro pomo;
        bool alertAtEndOfWork = Properties.Settings.Default.AlertAtEndOfWork;
        bool alertAtEndOfBreak = Properties.Settings.Default.AlertAtEndOfBreak;

        public PomoWindow()
        {
            InitializeComponent();
            this.controlRectangle.MouseLeftButtonDown += new MouseButtonEventHandler(controlRectangle_MouseLeftButtonDown);
            this.timeTextBox.MouseLeftButtonDown += delegate { DragMove(); if (this.Top < 20) { this.Top = 0; } };
            this.timeTextBox.MouseRightButtonDown += new MouseButtonEventHandler(timeTextBox_MouseRightButtonDown);
            this.xBorder.MouseLeftButtonDown += new MouseButtonEventHandler(xBorder_MouseLeftButtonDown);
            this.Top = Pomo.Properties.Settings.Default.PomoWindowPositionTop;
            this.Left = Pomo.Properties.Settings.Default.PomoWindowPositionLeft;
            pomo = (Pomodoro)stackPanel1.DataContext;
            pomo.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(pomo_PropertyChanged);
            pomo.DurationMinutes = Pomo.Properties.Settings.Default.PomodoroDuration;
            pomo.BreakDurationMinutes = Pomo.Properties.Settings.Default.BreakDuration;
        }

        void xBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Pomo.Properties.Settings.Default.PomoWindowPositionTop = (int)this.Top;
            Pomo.Properties.Settings.Default.PomoWindowPositionLeft = (int)this.Left;
            Pomo.Properties.Settings.Default.Save();
            this.Close();
        }

        void controlRectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pomo.StartPauseContinue();
        }

        void pomo_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Status")
            {
                if (pomo.Status == Pomodoro.StatusType.Breaking && pomo.PreviousStatus != Pomodoro.StatusType.BreakPaused )
                {
                    if (alertAtEndOfWork)
                        MessageBox.Show("Take a break!", "Break");
                }
                if (pomo.Status == Pomodoro.StatusType.Stopped && pomo.PreviousStatus == Pomodoro.StatusType.Breaking)
                {
                    if (alertAtEndOfBreak)
                        MessageBox.Show("Time to get back to work!", "Work");

                    alertAtEndOfWork = Properties.Settings.Default.AlertAtEndOfWork;
                    alertAtEndOfBreak = Properties.Settings.Default.AlertAtEndOfBreak;
                    pomo.DurationMinutes = Pomo.Properties.Settings.Default.PomodoroDuration;
                    pomo.BreakDurationMinutes = Pomo.Properties.Settings.Default.BreakDuration;
                }
            }

        }

        void timeTextBox_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            SettingsWindow settingsWindow = new SettingsWindow();
            settingsWindow.Top = this.Top + 20;
            settingsWindow.Left = Math.Min(System.Windows.SystemParameters.WorkArea.Right-settingsWindow.Width, this.Left);
            Nullable<bool> dialogResult = settingsWindow.ShowDialog();
            bool settingsWereChanged = (bool)dialogResult;
            string message = "Reset the timer and apply the new settings?" + Environment.NewLine
                           + "(If not, the settings will be applied when the current pomodoro is complete.)";
            if (settingsWereChanged)
            {
                // If the pomodoro is either stopped or the user okays it...
                if (pomo.Status == Pomodoro.StatusType.Stopped
                    || MessageBox.Show(message, "Reset Now?", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    // stop the timer...
                    pomo.Stop();
                    /// and apply the new settings.
                    pomo.DurationMinutes = Pomo.Properties.Settings.Default.PomodoroDuration;
                    pomo.BreakDurationMinutes = Pomo.Properties.Settings.Default.BreakDuration;
                    alertAtEndOfWork = Properties.Settings.Default.AlertAtEndOfWork;
                    alertAtEndOfBreak = Properties.Settings.Default.AlertAtEndOfBreak;
                }
            }
        }

    }
}
